/* Author: Nahuel F. Lema

 */

var informacionController = {

    getContent: {
        defs: {
            infoCategoryDetailsContainer: $('#info-category-details-container'),
            infoCategoryList: $('#info-category-list'),
            categoryList: $('#category-list'),
            defaultCategoryId: '1',
            infoContent: []
        },

        get: function(){
            var self = this;

            $.ajax({
                url: scriptsController.defs.baseUrl+'controllers/categoriaController/findAllAll',
                success: function (data) {
                    self.defs.infoContent = data;
                    if (data){
                        self.print();
                        self.dynamicUrlParser();
                    }
                }
            })
        },

        getCssClass: function(id){
            return this.defs.infoCategoryList.find('[data-category-id="'+id+'"]').attr('class').split(' ')[1];
        },

        setActive: function(id){
            var container = this.defs.infoCategoryList;
            container.find('li .link').removeClass('active');
            container.find('[data-category-id="'+id+'"] .link').addClass('active');
        },

        print: function () {
            var items = this.defs.infoContent,
            infoCategoryList = this.defs.infoCategoryList,
            categoryListTpl = document.getElementById('categoryListTpl').value,
            infoCategoryListTpl = document.getElementById('infoCategoryListTpl').value,
            item  = '', categoryListContent = '', infoCategoryListContent = '';

            for (var x in items['orden']) {
                item = items['categorias'][items['orden'][x]];
                categoryListContent += categoryListTpl;
                categoryListContent = categoryListContent.replace(/\$\{categoriaIdentificador\}/g, item.identificador)
                .replace(/\$\{categoriaId\}/g, item.id)
                .replace(/\$\{categoriaNombre\}/g, item.nombre)
                .replace(/\$\{jsOnclick\}/g, 'javascript:informacionController.map.bind(this); return false')
                .replace(/\$\{baseUrl\}/g, scriptsController.defs.baseUrl)
                .replace(/\$\{[a-zA-Z]+\}/g, '');


                infoCategoryListContent += infoCategoryListTpl
                infoCategoryListContent = infoCategoryListContent.replace(/\$\{categoriaIdentificador\}/g, item.identificador)
                .replace(/\$\{categoriaId\}/g, item.id)
                .replace(/\$\{categoriaNombre\}/g, item.nombre)
                .replace(/\$\{[a-zA-Z]+\}/g, '');
            }

            infoCategoryList.append(infoCategoryListContent);
        },

        printCategoryInfo: function(id){
            var items = this.defs.infoContent,
            container = this.defs.infoCategoryDetailsContainer,
            content = '' , itemClass = '', item;
            item = items['categorias'][id];
            itemClass = this.getCssClass(parseInt(item.id));

            content += '<div data-category-id="'+ parseInt(item.id) +'" class="'+ itemClass +' box-info">'
            + '<h3>'+item.nombre+'</h3>'
            + item.texto
            + '</div>';

            container.append(content);
            this.switcher(id);
        },

        switcher: function(id){
            var container = this.defs.infoCategoryDetailsContainer;
            container.find('.box-info').hide();
            container.find('[data-category-id="'+id+'"]').show();
            this.setActive(id);
        },

        check: function(id){
            var self = this,
            container = self.defs.infoCategoryDetailsContainer;
            id = parseInt(id);

            if (!!container.find('[data-category-id='+id+']').length) {
                self.switcher(id)
            } else {
                self.printCategoryInfo(id);
            }
        },

        bind: function(){
            var self = this;

            $('#info-category-list li a').die('click').live('click', function(e){
                e.preventDefault();
                self.check($(this).parents('.list-item').attr('data-category-id'));
                self.actualizarHash();
            });
        },
        
        //funcion para actualizar la url
        actualizarHash: function () {
            var self = this;
            var hash = '';
            var titulo = 'Reciclado de ';
            hash = $('#info-category-list li a.active').attr('href').replace('#', '');
            if (navigator.appName == "Microsoft Internet Explorer") {//si es explorer
                if (window.location.href.indexOf('#') == -1) {
		    document.location = scriptsController.defs.baseUrl+'informate/#!/'+hash;
		} else {
		    document.location.hash = '#!/'+hash;
		}
            } else {//si es otro navegador
                window.history.replaceState('', titulo, scriptsController.defs.baseUrl+'informate/'+hash);
            }
        },
        
        dynamicUrlParser: function () {
	    var url = document.location+'', id = 0;

	    url = url.replace(scriptsController.defs.baseUrl+'informate', '')
	    .replace('#!/', '')
            .replace(/^\//, '')
	    .replace(/\/$/, '');
            
            if (url) {
                var id = $('a[href=#'+url+']').parent().data('category-id')
                this.printCategoryInfo(id);
            } else {
                this.printCategoryInfo(this.defs.defaultCategoryId);
            }

            /*var categorias = url.split('/');
            if (!!categorias[0]) {
                for (var i in categorias) {
                    id = this.defs.idsCategorias[categorias[i]]
                    this.cargarXML(id);
                    $('#categoria'+id).addClass('active');
                }
                this.defs.lastCategoryActive = id;
                mapaController.getContent.check(id)
            }*/
		
	},

        init: function(){
            this.get();
            this.bind();
        }
    },
    
    blog: {
        defs: {
            cuantos: 3,
            destino: document.getElementById('listadoPosts'),
            imageSize: {
                ancho:138,
                alto:138
            }
        },
        init: function () {
            var self = this;
            $.ajax({
                url:scriptsController.defs.baseUrl+'resources/blog/php/controllers/listarPosts.controller.php',
                type:'post',
                data:'cuantos='+self.defs.cuantos,
                success:function (data) {
                    self.defs.destino.innerHTML = data;
                    $('.box-item').removeClass('span8').addClass('span12')
                }
            })
        }
    },

    init: function(){
        this.getContent.init();
        this.blog.init();
    }
}

$(function(){
    informacionController.init();
});